package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.po.RedEnvelope;
import com.letoken.platform.repo.app.po.RedEnvelopePage;
import com.letoken.platform.repo.app.po.Statistical;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2023-01-10
 */
public interface RedEnvelopesMapper extends BaseMapper<RedEnvelope> {

    @Select("select * from red_envelopes where id = #{id}  for update")
    RedEnvelope selectForUpdate(Integer id);


    @Select("select ifnull(sum(money),0) as sum from red_envelopes where owner = #{userId} and  type = #{type} and  created_at between #{startDatetime} and #{endDatetime}")
    Statistical countAndSumByUserIdAndTypeAndCreatedAt(@Param("userId") Integer userId, @Param("type") Integer type, @Param("startDatetime") LocalDateTime startDatetime, @Param("endDatetime") LocalDateTime endDatetime);

    @Select("select * from red_envelopes where status = 1 and created_at")
    List<RedEnvelope> selectAvailable();


    @Select("select * from red_envelopes where id = #{id} and owner = #{userId}")
    RedEnvelope selectOne(@Param("id") Integer id,@Param("userId") Integer userId);


    @Select("select ifnull(count(id),0) as count,ifnull(sum(money),0) as sum from red_envelopes where owner = #{userId}")
    Statistical countAndSumByUserId(@Param("userId") Integer userId);

    Page<RedEnvelopePage> page(@Param("page") Page<?> page, @Param("owner") Integer owner, @Param("startDatetime") LocalDateTime startDatetime, @Param("endDatetime") LocalDateTime endDatetime);
}
